<template>
  <div class="app-container">
    <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input clearable v-model="searchForm.keyword" placeholder="搜索"></el-input>
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="searchForm.type" placeholder="请选择" clearable>
          <el-option
            v-for="item in [{'id':1,'name':'注册'},{'id':2,'name':'登录'},{'id':3,'name':'找回密码'},{'id':0,'name':'通用'}]"
            :key="item.id" :label="item.name" :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="#" width="95">
        <template v-slot:default="{scope,row}">
          {{ row.id }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="验证码" width="150">
        <template v-slot:default="{scope,row}">
          {{ row.code }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" width="150">
        <template v-slot:default="{scope,row}">
          <el-tag v-if="row.status==1" type="success"> {{ row.status_text }}</el-tag>
          <el-tag v-if="row.status==0" type="danger"> {{ row.status_text }}</el-tag>

        </template>
      </el-table-column>
      <el-table-column align="center" label="类型" width="120">
        <template v-slot:default="{scope,row}">
          <el-tag v-if="row.type==1" type="success">注册</el-tag>
          <el-tag v-if="row.type==2" type="primary">登录</el-tag>
          <el-tag v-if="row.type==3" type="primary">找回密码</el-tag>
          <el-tag v-if="row.type==0" type="primary">通用</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="账号" width="180">
        <template v-slot:default="{scope,row}">
          {{ row.account }}
        </template>
      </el-table-column>

      <el-table-column label="发送时间" width="180" align="center">
        <template v-slot:default="{scope,row}">
          <span>{{ row.created_at }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="180" align="center">
        <template v-slot:default="{scope,row}">
          <span>{{ row.updated_at || '/' }}</span>
        </template>
      </el-table-column>

    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
                @pagination="fetchData"
    />
  </div>

</template>

<script>
import api from '@/api/code_log'
import Pagination from '@/components/Pagination'

export default {
  components: {
    Pagination
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '1': 'success',
        '0': 'gray',
        '-1': 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        sort: ''
      },
      searchForm: {
        keyword: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleCategorySelect(category) {
      this.searchForm.category_id = category
    },
    fetchData() {
      this.listLoading = true
      api.getList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    search() {
      this.listQuery.page = 1
      this.listQuery = Object.assign(this.listQuery, this.searchForm)
      this.fetchData()
    }
  }

}
</script>
